/**
 * autoSaveProvider module
 */

  import eventHelper from "../../utils/EventHelper";

  var tool = require("../../utils/tool");
  var consts = require("../../utils/consts");
  var storage = require("../../utils/storage");
  // var BaseWidgets = require("../model/BaseWidgets");
  var alertProvider = require("./alertProvider");
  var uuidHelper = require("../../utils/UuidHelper");

  var autoSaveProvider = (function (){
      var isSaving = false;
      var stopInterval = null;

      var permission = window.is.options.permission;

      var unSavedPageIds = tool.storage(consts.UNSAVED_PAGEIDS_KEY) || [];
      var autoSaveStorages = tool.storage(consts.UNSAVED_WIDGETS_KEY) || {};

      var LOGIN_SYMBOL = 'edtlg';
      var NO_LOGIN_URL = "/web/user/logout?next=/web/user/login?is_session_expired=1";

      var start = function (){
          stopInterval = setInterval(function (){
              save();
          }, consts.AUTO_SAVE_DURATION * 1000);
      };

      var stop = function (){
          clearInterval(stopInterval);
      };

      var update = function (object, params, callback){
          var key = object.id, page_id = params.page_id;

          object.uuid = object.uuid ? object.uuid : uuidHelper.generate();
          autoSaveStorages[key] = object;
          tool.storage(consts.UNSAVED_WIDGETS_KEY, autoSaveStorages);

          eventHelper.trigger(consts.EVENTS.REQUEST_CHANGED, ["add", object.uuid]);

          if (unSavedPageIds.indexOf(page_id) === -1){
              unSavedPageIds.push(page_id);
          }
          tool.storage(consts.UNSAVED_PAGEIDS_KEY, unSavedPageIds);

          callback && callback(object);
      };

      var hasUnSavedWidgets = function (){
          autoSaveStorages = tool.storage(consts.UNSAVED_WIDGETS_KEY) || {};
          return !tool.isEmptyObject(autoSaveStorages);
      };

      var getUnSavedWidgets = function (){
          return tool.storage(consts.UNSAVED_WIDGETS_KEY) || {};
      };

      var checkLoging = function (){
          if (!$.cookie(LOGIN_SYMBOL)){
              clearInterval(stopInterval);
              window.location = NO_LOGIN_URL;
              return false;
          }
          return true;
      };

      var clearUuids = function (uuids){
          uuids.forEach(function (uuid){
              eventHelper.trigger(consts.EVENTS.REQUEST_CHANGED, ["remove", uuid]);
          });
      };

      var save = function (callback){
          callback = callback || function (){};

          if (!checkLoging()) return;

          // admin查看AR进入不保存数据
          if (permission == consts.PERMISSION_READONLY){
              callback();
              return;
          }

          autoSaveStorages = tool.storage(consts.UNSAVED_WIDGETS_KEY) || {};

          if (!tool.isEmptyObject(autoSaveStorages) && !isSaving){
              console.log("自动保存中...");

              isSaving = true;

              var autoSaveWidgets = [], uuids = [];
              for (var key in autoSaveStorages){
                  var object = autoSaveStorages[key];
                  if (object.uuid) uuids.push(object.uuid);

                  delete object.uuid;

                  autoSaveWidgets.push(object);
              }

              BaseWidgets.save(autoSaveWidgets, function (){
                  callback();

                  console.log("保存成功");

                  isSaving = false;

                  alertProvider.closeByAlert(alert);

                  unSavedPageIds = [];
                  autoSaveStorages = {};

                  tool.storage(consts.UNSAVED_PAGEIDS_KEY, unSavedPageIds);
                  tool.storage(consts.UNSAVED_WIDGETS_KEY, autoSaveStorages);

                  clearUuids(uuids);
              }, function (){
                  callback();

                  console.log("保存失败");

                  isSaving = false;

                  alertProvider.closeByAlert(alert);

                  clearUuids(uuids);
              });
          } else {
              callback();
          }
      };

      return {
          save: save,
          stop: stop,
          start: start,
          update: update,
          hasUnSavedWidgets: hasUnSavedWidgets,
          getUnSavedWidgets: getUnSavedWidgets
      }
  })();

  module.exports = autoSaveProvider;
